package com.ojoin.trade.supplyloan_core.web.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlStattaskRunresultDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlStatetaskRunresultvo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlStattaskRunresult;
import com.ojoin.trade.supplyloan_core.web.service.TbSlStattaskRunresultService;

/**
 * Created with IntelliJ IDEA. User: Administrator Date: 15-10-27 Time: 下午4:46
 * To change this template use File | Settings | File Templates.
 */
@Service
public class TbSlStattaskRunresultServiceImpl extends
		BaseServiceImpl<TbSlStattaskRunresult> implements
		TbSlStattaskRunresultService {
	@Autowired
	private TbSlStattaskRunresultDao dao;

	@Override
	protected BaseDao<TbSlStattaskRunresult> getBaseDao() {
		return dao;
	}

	@Override
	public void insertTbSlStattaskRunresult(TbSlStattaskRunresult taskresultvo)
			throws BusinessException {
		((TbSlStattaskRunresultDao) this.getBaseDao())
				.insertTbSlStattaskRunresult(taskresultvo);
	}

	@Override
	public List<TbSlStattaskRunresult> queryTodayTbSltaskRunresultByJoinid(
			int joinid) throws BusinessException {
		return ((TbSlStattaskRunresultDao) this.getBaseDao())
				.queryTbSltaskRunresultByJoinidanddate(joinid,
						TimeUtils.getCurDate());
	}

	@Override
	public List<TbSlStatetaskRunresultvo> queryAllTbSltaskRunresultToday()
			throws BusinessException {
		return ((TbSlStattaskRunresultDao) this.getBaseDao())
				.queryAllTbSltaskRunresultBydate(TimeUtils.getCurDate());
	}

	@Override
	public List<TbSlStatetaskRunresultvo> queryAllFailureTbSltaskRunresultToday()
			throws BusinessException {
		return ((TbSlStattaskRunresultDao) this.getBaseDao())
				.queryAllFailureTbSltaskRunresultBydate(TimeUtils.getCurDate());
	}

	@Override
	public List<TbSlStatetaskRunresultvo> queryAllSuccessTbSltaskRunresultToday()
			throws BusinessException {
		return ((TbSlStattaskRunresultDao) this.getBaseDao())
				.queryAllSuccessTbSltaskRunresultBydate(TimeUtils.getCurDate());
	}

	@Override
	public List<TbSlStatetaskRunresultvo> queryAllTbSltaskRunresultToday(
			Pageable page) throws BusinessException {
		return ((TbSlStattaskRunresultDao) this.getBaseDao())
				.queryAllTbSltaskRunresultBydate(TimeUtils.getCurDate(), page);
	}

	@Override
	public List<TbSlStatetaskRunresultvo> queryByPlatformAndMerchantUserIDAndDate(
			String platformName, String idOfTbMerchantUser, String statDate,
			Pageable page) throws BusinessException {

		TbSlStattaskRunresult tbs = new TbSlStattaskRunresult();
		if (!CommonUtils.isEmpty(idOfTbMerchantUser)) {
			tbs.setIdOfTbMerchantUser(Integer.valueOf(idOfTbMerchantUser));
		}

		tbs.setPlatformName(platformName);
		tbs.setTaskExecDate(statDate);
		return ((TbSlStattaskRunresultDao) this.getBaseDao())
				.queryByPlatformAndMerchantUserIDAndDate(tbs, page);
	}

	@Override
	public TbSlStattaskRunresult getByPlatformUser(TbSlPlatformUser platformUser) {
		TbSlStattaskRunresult runResult = new TbSlStattaskRunresult();

		runResult.setIdOfPlatformUser(platformUser.getId());
		runResult.setIdOfTbMerchantUser(platformUser.getIdOfTbMerchantUser());
		runResult.setPlatformName(platformUser.getPlatformName());
		runResult.setTaskExecDate(TimeUtils.getCurDate());

		return runResult;
	}

	@Override
	public List<TbSlStattaskRunresult> queryFailureByJoinid(int joinid)
			throws BusinessException {
		return ((TbSlStattaskRunresultDao) this.getBaseDao())
				.queryFailureByJoinid(joinid, TimeUtils.getCurDate());
	}

	@Override
	public void deleteByPlateFormAndTime(int plateformId, String time)
			throws BusinessException {
		dao.deleteByPlateFormAndTime(plateformId, time);
	}

	@Override
	public void deleteAndAdd(TbSlStattaskRunresult stattaskRunresult)
			throws BusinessException {
		this.deleteByPlateFormAndTime(stattaskRunresult.getIdOfPlatformUser(),
				stattaskRunresult.getTaskExecDate());
		this.insertTbSlStattaskRunresult(stattaskRunresult);

	}

	@Override
	public void updateByIdOfPlatformUserAndtaskExecDate(
			TbSlStattaskRunresult stattaskRunresult) throws BusinessException {
		dao.updateByIdOfPlatformUserAndtaskExecDate(stattaskRunresult);

	}

	@Override
	public boolean isCatchSucceed(TbSlStatetaskRunresultvo tb) {
		if (null != tb) {
			if (null != tb.getLoginSuccessFlag()
					&& 1 == tb.getLoginSuccessFlag()
					&& null != tb.getSupplyInfoCorrectFlag()
					&& 1 == tb.getSupplyInfoCorrectFlag()
					&& null != tb.getSettleAccountCorrectFlag()
					&& 1 == tb.getSettleAccountCorrectFlag()
					&& null != tb.getGetDataSuccessFlag()
					&& 1 == tb.getGetDataSuccessFlag()) {

				return true;
			}
		}

		return false;
	}

	@Override
	public boolean isCatchSucceed(TbSlStattaskRunresult tbsl) {
		if (null != tbsl) {
			if (null != tbsl.getLoginSuccessFlag()
					&& 1 == tbsl.getLoginSuccessFlag()
					&& null != tbsl.getSupplyInfoCorrectFlag()
					&& 1 == tbsl.getSupplyInfoCorrectFlag()
					&& null != tbsl.getSettleAccountCorrectFlag()
					&& 1 == tbsl.getSettleAccountCorrectFlag()
					&& null != tbsl.getGetDataSuccessFlag()
					&& 1 == tbsl.getGetDataSuccessFlag()) {

				return true;
			}
		}

		return false;
		
		
		
	}
}
